package com.huijz.flywaydemo.config;

import org.flywaydb.core.Flyway;
import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
public class FlywayConfig {

    /**
     * 演示：在开发环境中，每次启动应用时先清理数据库再迁移
     */
    @Bean
    @Profile("dev")
    public FlywayMigrationStrategy cleanMigrateStrategy() {
        return flyway -> {
            flyway.clean();
            flyway.migrate();
        };
    }
    
    /**
     * 演示：自定义Flyway配置
     * 注意：这个Bean会覆盖Spring Boot的自动配置
     * 实际项目中通常使用application.yml配置即可
     */
    @Bean
    @Profile("custom")
    public Flyway customFlyway() {
        return Flyway.configure()
                .dataSource("jdbc:h2:mem:custom_flyway_demo", "sa", "")
                .locations("classpath:db/migration", "classpath:db/custom")
                .baselineOnMigrate(true)
                .baselineVersion("0")
                .outOfOrder(true)
                .validateOnMigrate(true)
                .load();
    }
}